package com.iotimc.attendancesystem.util;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.text.Html;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.iotimc.attendancesystem.R;


/**
 * Created by Chenwj .
 *
 * @author cwj
 */

public class CustomDialog extends Dialog {

    private CustomDialog(Context context, int theme) {
        super(context, theme);
    }

    @Override
    public boolean onTouchEvent(@NonNull MotionEvent event) {
        // TODO Auto-generated method stub
        if(event.getAction() == MotionEvent.ACTION_DOWN){
            if(getCurrentFocus()!=null && getCurrentFocus().getWindowToken()!=null){
                InputMethodManager manager = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                if (manager != null) {
                    manager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
                }
            }
        }
        return super.onTouchEvent(event);
    }

    /**
     * 生成dialog的帮助类
     */
    public static class Builder {

        private Context context;

        private String title, message, positiveButtonText, negativeButtonText;
        private View contentView;

        private OnClickListener positiveButtonClickListener, negativeButtonClickListener;


        public Builder(Context context) {
            this.context = context;
        }

        /**
         * 设置dialog的内容文字信息
         *
         * @param message 内容文字信息
         * @return 帮助类实例
         */
        public Builder setContent(@Nullable String message) {
            this.message = message;
            return this;
        }


        /**
         * 设置dialog的标题文字信息,！！！！方法暂时无用！！！！
         *
         * @param title 标题文字信息
         * @return 帮助类实例
         */
        public Builder setTitle(String title) {
            this.title = title;
            return this;
        }

        /**
         * 设置自定义view到dialog的内容位置，如果消息内容已经设置，自定义view将不会被加载
         *
         * @param v 自定义的view
         * @return 帮助类实例
         */
        public Builder setContentView(View v) {
            this.contentView = v;
            this.message = null;
            return this;
        }

        /**
         * 设置确定按钮的listener和文字信息
         *
         * @param positiveButtonText 确定按钮的文字信息
         * @param listener           确定按钮的监听
         * @return 帮助类实例
         */
        public Builder setPositiveButton(String positiveButtonText, OnClickListener listener) {
            this.positiveButtonText = positiveButtonText;
            this.positiveButtonClickListener = listener;
            return this;
        }


        /**
         * 设置取消按钮的listener和文字信息
         *
         * @param negativeButtonText 取消按钮的文字信息
         * @param listener           取消按钮的监听
         * @return 帮助类实例
         */
        public Builder setNegativeButton(String negativeButtonText, OnClickListener listener) {
            this.negativeButtonText = negativeButtonText;
            this.negativeButtonClickListener = listener;
            return this;
        }

        /**
         * 设置RecyclerView到dialog的内容位置，如果消息内容已经设置，自定义view将不会被加载
         *
         * @param adapter RecyclerView.Adapter
         * @param manager RecyclerView.LayoutManager
         * @param layout  RecyclerView layout
         * @return 帮助类实例
         */
        public Builder recyContentView(RecyclerView.Adapter adapter, RecyclerView.LayoutManager manager, int layout) {
            LayoutInflater inflater = LayoutInflater.from(context);
            RecyclerView mRecyclerView = (RecyclerView) inflater.inflate(layout, null);
            //设置布局管理器
            mRecyclerView.setLayoutManager(manager);
            mRecyclerView.setHasFixedSize(true);
            mRecyclerView.setAdapter(adapter);
            this.setContentView(mRecyclerView);
            return this;
        }


        /**
         * 创建dialog
         */
        private int dialogStyle = R.style.customDialog;
        private int dialogLayout = R.layout.dialog_layout;

        public CustomDialog build() {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            // 加载自定义布局
            final CustomDialog dialog = new CustomDialog(context, dialogStyle);
            dialog.setCanceledOnTouchOutside(false);

            View layout = null;
            if (inflater != null) {
                layout = inflater.inflate(dialogLayout, null);
            }
            if (layout != null) {
                dialog.addContentView(layout, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
            }

            //设置确定按钮
            Button bt = null;
            if (layout != null) {
                bt = (Button) layout.findViewById(R.id.positiveButton);
            }
            if (!TextUtils.isEmpty(positiveButtonText) && positiveButtonClickListener != null) {
                if (bt != null) {
                    bt.setText(positiveButtonText);
                }

                if (bt != null) {
                    bt.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            positiveButtonClickListener.onClick(dialog, DialogInterface.BUTTON_POSITIVE);
                        }
                    });
                }

            } else {
                //没设置按钮文字信息和按钮点击事件，按钮将隐藏
                if (bt != null) {
                    bt.setClickable(false);
                }
                if (bt != null) {
                    bt.setText(null);
                }
            }

            // 设置取消按钮
            Button bt1 = null;
            if (layout != null) {
                bt1 = (Button) layout.findViewById(R.id.negativeButton);
            }
            if (!TextUtils.isEmpty(negativeButtonText) && negativeButtonClickListener != null) {
                if (bt1 != null) {
                    bt1.setText(negativeButtonText);
                }

                if (bt1 != null) {
                    bt1.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            negativeButtonClickListener.onClick(dialog, DialogInterface.BUTTON_NEGATIVE);
                        }
                    });
                }

            } else {
                //没设置按钮文字信息和按钮点击事件，按钮将隐藏
                if (bt1 != null) {
                    bt1.setClickable(false);
                }
                if (bt1 != null) {
                    bt1.setText(null);
                }
            }

            //设置内容
            if (message != null) {
                if (layout != null) {
                    ((TextView) layout.findViewById(R.id.message)).setText(Html.fromHtml(message));
                }


                //TODO 用于文字两端对齐
                // 设置webview不允许竖直滚动
                //WebView wv = (WebView) layout.findViewById(R.id.message)
                //wv.setVerticalScrollBarEnabled(false)

                //wv.loadDataWithBaseURL("",
                // "<![CDATA[<html> <head></head> <body style=\"background-color:"
                //         + "#FFFFFF" + ";text-align:justify;font-size:"
                //         + "24" + "px;color:" + "#323232"
                //         + ";text-indent:2em;\"> " + message
                //       + " <p> </body></html>", "text/html", "utf-8", "")

            } else if (contentView != null) {

                if (layout != null) {
                    ((LinearLayout) layout.findViewById(R.id.content)).removeAllViews();
                }
                //加载自定义view
                if (layout != null) {
                    ((LinearLayout) layout.findViewById(R.id.content)).addView(contentView, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
                }
            }
            if (layout != null) {
                dialog.setContentView(layout);
            }



            dialog.dismiss();
            dialog.show();

            return dialog;
        }
    }
}




